package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.SellerCategory;
import cn.signalpha.modules.shop.service.SellerCategoryService;
import cn.signalpha.modules.shop.service.SellerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/seller/category")
@Api(value = "商户分类", tags = "商户分类")
public class SellerCategoryController extends BaseController {

	private final SellerService sellerService;
	private final SellerCategoryService sellerCategoryService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<SellerCategory>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<SellerCategory> queryWrapper = Condition.getQueryWrapper(params, SellerCategory.class);
		queryWrapper.lambda().orderByDesc(SellerCategory::getSort).orderByDesc(SellerCategory::getId);
        return R.data(sellerCategoryService.page(Condition.getPage(query), queryWrapper));
    }

	@GetMapping("/list")
	@ApiOperation(value = "列表", notes = "")
	public R<List<SellerCategory>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		QueryWrapper<SellerCategory> queryWrapper = Condition.getQueryWrapper(params, SellerCategory.class);
		queryWrapper.lambda().orderByDesc(SellerCategory::getSort).orderByDesc(SellerCategory::getId);
		return R.data(sellerCategoryService.list(queryWrapper));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<SellerCategory> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(sellerCategoryService.getById(id));
	}

    @PostMapping("/submit")
    @ApiOperation(value = "提交", notes = "传入sellerCategory")
    public R submit(@Valid @RequestBody SellerCategory sellerCategory) {
        return R.status(sellerCategoryService.saveOrUpdate(sellerCategory));
    }

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toLongList(ids).forEach(id -> {
			if(sellerService.countByParams(Kv.create().set("categoryId", id)) > 0){
				throw new ServiceException("分类存在商户，无法删除");
			}
			sellerCategoryService.removeById(id);
		});
		return R.status(true);
	}

}
